package us.gaaoc.framework.webapp.backingbeans.admin;

import java.util.ArrayList;
import java.util.UUID;

import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectMany;
import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import us.gaaoc.framework.manager.OrganizationManager;
import us.gaaoc.framework.manager.RoleManager;
import us.gaaoc.framework.model.Organizations;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonOrganizations;
import us.gaaoc.framework.model.Profile;
import us.gaaoc.framework.model.Roles;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

public class AddUser extends BaseBackingBean {

	private OrganizationManager organizationManager;
	private RoleManager roleManager;
	
	// Person Properties 
	private String userId;
    private String password;
    private String firstName;
    private String lastName;
    private byte changePwdFlag;
    private String email;
    
    // Selectboxes for roles and organizations
    private UISelectMany organizationsSelect;
    private UISelectMany rolesSelect;
    
    /**
     * Add user constructor. Initilizes multiselect list for roles 
     * and organizations. 
     *
     */
    public AddUser() {
       	this.organizationManager = new OrganizationManager();
    	this.roleManager = new RoleManager(false);
    	
    	this.organizationsSelect = new UISelectMany();
    	for (Organizations organization : this.organizationManager.getAllOrganizations()) {
    		SelectItem item = new SelectItem(organization.getUuid(), organization.getName() );
            UISelectItem uiItem = new UISelectItem();
            uiItem.setValue( item );
            
            this.organizationsSelect.getChildren().add( uiItem );
    	}
    	
    	this.rolesSelect = new UISelectMany();
    	for (Roles role : this.roleManager.getAllRoles()) {
			SelectItem item = new SelectItem(role.getUuid(), role.getRole() );
            UISelectItem uiItem = new UISelectItem();
            uiItem.setValue( item );            
            this.rolesSelect.getChildren().add( uiItem );
		}
    }
    
    /**
     * This method adds the new user into the database and sends the email
     * notification to that user.
     * @return
     * @throws Exception
     */
    public String addUser() throws  Exception{
    	Person newPerson = new Person();
    	newPerson.setUserId(getUserId());
    	newPerson.setPassword("password");
    	newPerson.setFirstName(getFirstName());
    	newPerson.setLastName(getLastName());
    	newPerson.setChangePwdFlag((byte)0);
    	newPerson.setOrganizations(new ArrayList<PersonOrganizations>());
    	newPerson.setRoles(new ArrayList<Roles>());    	
    	
    	Object[] selectedRoleUuids =  this.rolesSelect.getSelectedValues();
    	for (Object current : selectedRoleUuids) {
    		System.out.println(Roles.class);
    		Roles aRole = (Roles) this.getModelFacade().findRoleById(current.toString());
    	
    		newPerson.getRoles().add(aRole);
    		aRole.getPeople().add(newPerson);
    	}
    	
    	this.getModelFacade().makePersistent(newPerson);
    	
    	Object[] selectedOrganizationsUuids =  this.organizationsSelect.getSelectedValues();
    	for (Object current : selectedOrganizationsUuids) {
    		Organizations aOrganization = (Organizations)this.getModelFacade().findOrganizationById(current.toString()); 
    		
    		PersonOrganizations po = new PersonOrganizations();
    		po.setOrganization(aOrganization);
    		po.setPerson(newPerson);
    		po.setUuid(UUID.randomUUID().toString());
    		po.setCourtUuid(aOrganization.getUuid());
    		newPerson.getOrganizations().add(po);
    		aOrganization.getPeople().add(po);
    		this.getModelFacade().makePersistent(po);
    	}
    	
    	Profile userProfile = new Profile();
    	userProfile.setEmail(getEmail());
    	userProfile.setPerson(newPerson);
    	
    	this.getModelFacade().makePersistent(newPerson);
    	this.getModelFacade().makePersistenet(userProfile);
    	
    	newPerson.setProfile(userProfile);
    	this.getModelFacade().makePersistent(newPerson);    	
    	
//    	EmailNotification emailNotification = new EmailNotification();
//    	String message = "Here are your requested information for http://localhost:8080/GAJE_BibbCounty_1.0/client.faces"
//			+ "\n" + "User Name: " + getUserId() + "\n"
//			+ "Password: " + "password";
    	//emailNotification.sendUserIdByMail(getEmail(),"Email Notification",message);
    	
    	return "add";
    }
    
	/**
	 * @return Returns the changePwdFlag.
	 */
	public byte getChangePwdFlag() {
		return changePwdFlag;
	}

	/**
	 * @param changePwdFlag The changePwdFlag to set.
	 */
	public void setChangePwdFlag(byte changePwdFlag) {
		this.changePwdFlag = changePwdFlag;
	}

	/**
	 * @return Returns the firstName.
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName The firstName to set.
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return Returns the lastName.
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param lastName The lastName to set.
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * @return Returns the password.
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password The password to set.
	 */
	public void setPassword(String password) {
		this.password = password;
	}


	/**
	 * @return Returns the userId.
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * @param userId The userId to set.
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * 
	 * @return Returns the roleSelect
	 */
	public UISelectMany getRolesSelect() {
		return rolesSelect;
	}

	/**
	 * 
	 * @param rolesSelect The roleSelect to set
	 */
	public void setRolesSelect(UISelectMany rolesSelect) {
		this.rolesSelect = rolesSelect;
	}

	/**
	 * 
	 * @return Returns the organizationsSelect
	 */
	public UISelectMany getOrganizationsSelect() {
		return organizationsSelect;
	}

	/**
	 * 
	 * @param organizationsSelect The organizationsSelect to set
	 */
	public void setOrganizationsSelect(UISelectMany organizationsSelect) {
		this.organizationsSelect = organizationsSelect;
	}
	
	/**
	 * This method navigates user to the previous page (adminConsole).
	 * @return String
	 */
	public String back(){
		return "adminConsole";
	}

	/**
	 * @return Returns the email.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email The email to set.
	 */
	public void setEmail(String email) {
		this.email = email;
	}
	
}
